﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.SqlClient;

namespace TB.DALSqlProvider
{
    /// <summary>
    /// 对*MpReqMsg*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class MpReqMsgSqlDALProvider:IDAL.IDALMpReqMsg
    { 
        public int Insert(TB.Entity.MpReqMsg obj)
        {
           //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpReqMsg_Insert") as SqlCommand;
            //3.cmd参数赋值
            //cmd.Parameters.AddWithValue("@id_auto",(obj.id_auto==0||obj.id_auto==null)?DBNull.Value:(object)obj.id_auto);
            cmd.Parameters.AddWithValue("@toUserName",(obj.toUserName==String.Empty||obj.toUserName==null)?DBNull.Value:(object)obj.toUserName);
            cmd.Parameters.AddWithValue("@fromUserName",(obj.fromUserName==String.Empty||obj.fromUserName==null)?DBNull.Value:(object)obj.fromUserName);
            cmd.Parameters.AddWithValue("@createTime",(obj.createTime==String.Empty||obj.createTime==null)?DBNull.Value:(object)obj.createTime);
            cmd.Parameters.AddWithValue("@msgType",(obj.msgType==String.Empty||obj.msgType==null)?DBNull.Value:(object)obj.msgType);
            cmd.Parameters.AddWithValue("@text_Content",(obj.text_Content==String.Empty||obj.text_Content==null)?DBNull.Value:(object)obj.text_Content);
            cmd.Parameters.AddWithValue("@image_PicUrl",(obj.image_PicUrl==String.Empty||obj.image_PicUrl==null)?DBNull.Value:(object)obj.image_PicUrl);
            cmd.Parameters.AddWithValue("@location_Location_X",(obj.location_Location_X==String.Empty||obj.location_Location_X==null)?DBNull.Value:(object)obj.location_Location_X);
            cmd.Parameters.AddWithValue("@location_Location_Y",(obj.location_Location_Y==String.Empty||obj.location_Location_Y==null)?DBNull.Value:(object)obj.location_Location_Y);
            cmd.Parameters.AddWithValue("@location_Scale",(obj.location_Scale==String.Empty||obj.location_Scale==null)?DBNull.Value:(object)obj.location_Scale);
            cmd.Parameters.AddWithValue("@location_Label",(obj.location_Label==String.Empty||obj.location_Label==null)?DBNull.Value:(object)obj.location_Label);
            cmd.Parameters.AddWithValue("@link_Title",(obj.link_Title==String.Empty||obj.link_Title==null)?DBNull.Value:(object)obj.link_Title);
            cmd.Parameters.AddWithValue("@link_Description",(obj.link_Description==String.Empty||obj.link_Description==null)?DBNull.Value:(object)obj.link_Description);
            cmd.Parameters.AddWithValue("@link_Url",(obj.link_Url==String.Empty||obj.link_Url==null)?DBNull.Value:(object)obj.link_Url);
            cmd.Parameters.AddWithValue("@voice_MediaId",(obj.voice_MediaId==String.Empty||obj.voice_MediaId==null)?DBNull.Value:(object)obj.voice_MediaId);
            cmd.Parameters.AddWithValue("@voice_Format",(obj.voice_Format==String.Empty||obj.voice_Format==null)?DBNull.Value:(object)obj.voice_Format);
            cmd.Parameters.AddWithValue("@voice_Recognition",(obj.voice_Recognition==String.Empty||obj.voice_Recognition==null)?DBNull.Value:(object)obj.voice_Recognition);
            cmd.Parameters.AddWithValue("@video_MediaId",(obj.video_MediaId==String.Empty||obj.video_MediaId==null)?DBNull.Value:(object)obj.video_MediaId);
            cmd.Parameters.AddWithValue("@video_ThumbMediaId",(obj.video_ThumbMediaId==String.Empty||obj.video_ThumbMediaId==null)?DBNull.Value:(object)obj.video_ThumbMediaId);
            cmd.Parameters.AddWithValue("@r1",(obj.r1==String.Empty||obj.r1==null)?DBNull.Value:(object)obj.r1);
            cmd.Parameters.AddWithValue("@r2",(obj.r2==String.Empty||obj.r2==null)?DBNull.Value:(object)obj.r2);
            cmd.Parameters.AddWithValue("@r3",(obj.r3==String.Empty||obj.r3==null)?DBNull.Value:(object)obj.r3);
            //4.重要:通过db来执行这个cmd (在db内部对cmd进行构造)
           return db.ExecuteNonQuery(cmd);
        }
        
        public int Update(TB.Entity.MpReqMsg obj)
        {
            //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpReqMsg_Update") as SqlCommand;
            //3.cmd参数赋值
            //cmd.Parameters.AddWithValue("@id_auto",(obj.id_auto==0||obj.id_auto==null)?DBNull.Value:(object)obj.id_auto);
            cmd.Parameters.AddWithValue("@toUserName",(obj.toUserName==String.Empty||obj.toUserName==null)?DBNull.Value:(object)obj.toUserName);
            cmd.Parameters.AddWithValue("@fromUserName",(obj.fromUserName==String.Empty||obj.fromUserName==null)?DBNull.Value:(object)obj.fromUserName);
            cmd.Parameters.AddWithValue("@createTime",(obj.createTime==String.Empty||obj.createTime==null)?DBNull.Value:(object)obj.createTime);
            cmd.Parameters.AddWithValue("@msgType",(obj.msgType==String.Empty||obj.msgType==null)?DBNull.Value:(object)obj.msgType);
            cmd.Parameters.AddWithValue("@text_Content",(obj.text_Content==String.Empty||obj.text_Content==null)?DBNull.Value:(object)obj.text_Content);
            cmd.Parameters.AddWithValue("@image_PicUrl",(obj.image_PicUrl==String.Empty||obj.image_PicUrl==null)?DBNull.Value:(object)obj.image_PicUrl);
            cmd.Parameters.AddWithValue("@location_Location_X",(obj.location_Location_X==String.Empty||obj.location_Location_X==null)?DBNull.Value:(object)obj.location_Location_X);
            cmd.Parameters.AddWithValue("@location_Location_Y",(obj.location_Location_Y==String.Empty||obj.location_Location_Y==null)?DBNull.Value:(object)obj.location_Location_Y);
            cmd.Parameters.AddWithValue("@location_Scale",(obj.location_Scale==String.Empty||obj.location_Scale==null)?DBNull.Value:(object)obj.location_Scale);
            cmd.Parameters.AddWithValue("@location_Label",(obj.location_Label==String.Empty||obj.location_Label==null)?DBNull.Value:(object)obj.location_Label);
            cmd.Parameters.AddWithValue("@link_Title",(obj.link_Title==String.Empty||obj.link_Title==null)?DBNull.Value:(object)obj.link_Title);
            cmd.Parameters.AddWithValue("@link_Description",(obj.link_Description==String.Empty||obj.link_Description==null)?DBNull.Value:(object)obj.link_Description);
            cmd.Parameters.AddWithValue("@link_Url",(obj.link_Url==String.Empty||obj.link_Url==null)?DBNull.Value:(object)obj.link_Url);
            cmd.Parameters.AddWithValue("@voice_MediaId",(obj.voice_MediaId==String.Empty||obj.voice_MediaId==null)?DBNull.Value:(object)obj.voice_MediaId);
            cmd.Parameters.AddWithValue("@voice_Format",(obj.voice_Format==String.Empty||obj.voice_Format==null)?DBNull.Value:(object)obj.voice_Format);
            cmd.Parameters.AddWithValue("@voice_Recognition",(obj.voice_Recognition==String.Empty||obj.voice_Recognition==null)?DBNull.Value:(object)obj.voice_Recognition);
            cmd.Parameters.AddWithValue("@video_MediaId",(obj.video_MediaId==String.Empty||obj.video_MediaId==null)?DBNull.Value:(object)obj.video_MediaId);
            cmd.Parameters.AddWithValue("@video_ThumbMediaId",(obj.video_ThumbMediaId==String.Empty||obj.video_ThumbMediaId==null)?DBNull.Value:(object)obj.video_ThumbMediaId);
            cmd.Parameters.AddWithValue("@r1",(obj.r1==String.Empty||obj.r1==null)?DBNull.Value:(object)obj.r1);
            cmd.Parameters.AddWithValue("@r2",(obj.r2==String.Empty||obj.r2==null)?DBNull.Value:(object)obj.r2);
            cmd.Parameters.AddWithValue("@r3",(obj.r3==String.Empty||obj.r3==null)?DBNull.Value:(object)obj.r3);
            //4.重要:通过db来执行这个cmd (在db内部对cmd进行构造)
            return db.ExecuteNonQuery(cmd);
        }
        
        public int Delete(TB.Entity.MpReqMsg obj)
        {
            //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpReqMsg_Delete") as SqlCommand;
            //3.cmd参数赋值
           cmd.Parameters.AddWithValue("@id_auto", obj.id_auto);
            //4.重要:通过db来执行这个cmd (在db内部对cmd进行构造)
            return db.ExecuteNonQuery(cmd);
        }
        
        public List<TB.Entity.MpReqMsg> LoadAll()
        {
            //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpReqMsg_LoadAll") as SqlCommand;
            //3.通过db执行一个DBReader
            var reader =  db.ExecuteReader(cmd);
            //4.遍历DBReader,生成新的Entity对象,然后添加到List
            List<TB.Entity.MpReqMsg> list = new List<TB.Entity.MpReqMsg>();
            while (reader.Read())
            {
               list.Add(new TB.Entity.MpReqMsg()
               {
                    id_auto=reader["id_auto"]==DBNull.Value?0:(int)reader["id_auto"],
                    toUserName=reader["toUserName"]==DBNull.Value?String.Empty:(string)reader["toUserName"],
                    fromUserName=reader["fromUserName"]==DBNull.Value?String.Empty:(string)reader["fromUserName"],
                    createTime=reader["createTime"]==DBNull.Value?String.Empty:(string)reader["createTime"],
                    msgType=reader["msgType"]==DBNull.Value?String.Empty:(string)reader["msgType"],
                    text_Content=reader["text_Content"]==DBNull.Value?String.Empty:(string)reader["text_Content"],
                    image_PicUrl=reader["image_PicUrl"]==DBNull.Value?String.Empty:(string)reader["image_PicUrl"],
                    location_Location_X=reader["location_Location_X"]==DBNull.Value?String.Empty:(string)reader["location_Location_X"],
                    location_Location_Y=reader["location_Location_Y"]==DBNull.Value?String.Empty:(string)reader["location_Location_Y"],
                    location_Scale=reader["location_Scale"]==DBNull.Value?String.Empty:(string)reader["location_Scale"],
                    location_Label=reader["location_Label"]==DBNull.Value?String.Empty:(string)reader["location_Label"],
                    link_Title=reader["link_Title"]==DBNull.Value?String.Empty:(string)reader["link_Title"],
                    link_Description=reader["link_Description"]==DBNull.Value?String.Empty:(string)reader["link_Description"],
                    link_Url=reader["link_Url"]==DBNull.Value?String.Empty:(string)reader["link_Url"],
                    voice_MediaId=reader["voice_MediaId"]==DBNull.Value?String.Empty:(string)reader["voice_MediaId"],
                    voice_Format=reader["voice_Format"]==DBNull.Value?String.Empty:(string)reader["voice_Format"],
                    voice_Recognition=reader["voice_Recognition"]==DBNull.Value?String.Empty:(string)reader["voice_Recognition"],
                    video_MediaId=reader["video_MediaId"]==DBNull.Value?String.Empty:(string)reader["video_MediaId"],
                    video_ThumbMediaId=reader["video_ThumbMediaId"]==DBNull.Value?String.Empty:(string)reader["video_ThumbMediaId"],
                    r1=reader["r1"]==DBNull.Value?String.Empty:(string)reader["r1"],
                    r2=reader["r2"]==DBNull.Value?String.Empty:(string)reader["r2"],
                    r3=reader["r3"]==DBNull.Value?String.Empty:(string)reader["r3"],
               });
           }
           return list;
        }
        
        

        public TB.Entity.MpReqMsg LoadById(int id_auto)
        {
            //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpReqMsg_LoadById") as SqlCommand;
            //3.通过db执行一个DBReader
            cmd.Parameters.AddWithValue("@id_auto", id_auto);
            var reader = db.ExecuteReader(cmd);
            //4.遍历DBReader,生成一个新的Entity对象 
            TB.Entity.MpReqMsg entity = null;
            while (reader.Read())
            {
                entity = new TB.Entity.MpReqMsg()
                {
                    id_auto=reader["id_auto"]==DBNull.Value?0:(int)reader["id_auto"],
                    toUserName=reader["toUserName"]==DBNull.Value?String.Empty:(string)reader["toUserName"],
                    fromUserName=reader["fromUserName"]==DBNull.Value?String.Empty:(string)reader["fromUserName"],
                    createTime=reader["createTime"]==DBNull.Value?String.Empty:(string)reader["createTime"],
                    msgType=reader["msgType"]==DBNull.Value?String.Empty:(string)reader["msgType"],
                    text_Content=reader["text_Content"]==DBNull.Value?String.Empty:(string)reader["text_Content"],
                    image_PicUrl=reader["image_PicUrl"]==DBNull.Value?String.Empty:(string)reader["image_PicUrl"],
                    location_Location_X=reader["location_Location_X"]==DBNull.Value?String.Empty:(string)reader["location_Location_X"],
                    location_Location_Y=reader["location_Location_Y"]==DBNull.Value?String.Empty:(string)reader["location_Location_Y"],
                    location_Scale=reader["location_Scale"]==DBNull.Value?String.Empty:(string)reader["location_Scale"],
                    location_Label=reader["location_Label"]==DBNull.Value?String.Empty:(string)reader["location_Label"],
                    link_Title=reader["link_Title"]==DBNull.Value?String.Empty:(string)reader["link_Title"],
                    link_Description=reader["link_Description"]==DBNull.Value?String.Empty:(string)reader["link_Description"],
                    link_Url=reader["link_Url"]==DBNull.Value?String.Empty:(string)reader["link_Url"],
                    voice_MediaId=reader["voice_MediaId"]==DBNull.Value?String.Empty:(string)reader["voice_MediaId"],
                    voice_Format=reader["voice_Format"]==DBNull.Value?String.Empty:(string)reader["voice_Format"],
                    voice_Recognition=reader["voice_Recognition"]==DBNull.Value?String.Empty:(string)reader["voice_Recognition"],
                    video_MediaId=reader["video_MediaId"]==DBNull.Value?String.Empty:(string)reader["video_MediaId"],
                    video_ThumbMediaId=reader["video_ThumbMediaId"]==DBNull.Value?String.Empty:(string)reader["video_ThumbMediaId"],
                    r1=reader["r1"]==DBNull.Value?String.Empty:(string)reader["r1"],
                    r2=reader["r2"]==DBNull.Value?String.Empty:(string)reader["r2"],
                    r3=reader["r3"]==DBNull.Value?String.Empty:(string)reader["r3"],
                };
            }
            return entity;
        }
        public System.Data.DataSet GetDataSet(string strSql)
        {
            Database db = DatabaseFactory.CreateDatabase();
            var cmd = db.GetSqlStringCommand(strSql) as SqlCommand;
            System.Data.DataSet ds = db.ExecuteDataSet(cmd);
            return ds;
        }
    }
}